package com.blogs.service;

import java.net.URLEncoder;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.service.GetMsgTask.onTaskCallBack;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FeedBackTask {
	public interface IMsgCallBack {
		public void onSuccess();
		public void onFailed();
	}
	private String url=AppConfig.GET_FEED_BACK_RESULT;
	private GetMsgTask getresult;
	private IMsgCallBack callback;
	public FeedBackTask(String content,IMsgCallBack callback)
	{
		this.callback=callback;
		content=URLEncoder.encode(content);
		url=url.replace("{0}", content);
		getresult=new GetMsgTask(taskCallBack);
	}
	public void GetResult()
	{
		getresult.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack=new onTaskCallBack() {
		
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<String> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			if(msg.data.equals("1"))
			{
				callback.onSuccess();
			}
			else
			{
				callback.onFailed();
			}
		}
		
		public void onFailed() {
			// TODO Auto-generated method stub
			callback.onFailed();
		}
	};
}
